package net.zjitc.android2019.ch6service;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import net.zjitc.android2019.R;

/**
 * Activity：开启服务、关闭服务
 */
public class Ch6Service1Activity extends AppCompatActivity implements View.OnClickListener {
    Button btn_start, btn_stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch6_service1);
        btn_start = findViewById(R.id.ch6_service1_btn1);
        btn_stop = findViewById(R.id.ch6_service1_btn2);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ch6_service1_btn1:
                /* 开启服务:
                传递参数“意图”：从哪个Activity开启哪个服务*/
                Intent intent=new Intent(Ch6Service1Activity.this,MyService.class);
                startService(intent);
                /* Toast: 参数1 上下文（当前Activity）；
                *  参数2：显示的内容
                *  参数3：显示时间（长短）*/
                Toast.makeText(this, "服务已开启", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ch6_service1_btn2:
                /* 关闭服务*/
                Intent intent2=new Intent(Ch6Service1Activity.this,MyService.class);
                stopService(intent2);
                Toast.makeText(this, "服务已关闭", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
